auto();

toast("开始");

LOOPMAX = 90;
LOOP = LOOPMAX;

const HEIGHT = 2340;
const WIDTH = 1080;
setScreenMetrics(WIDTH, HEIGHT);

function rand(start, end) {
    let ret = start + Math.floor(Math.random() * Math.abs(end - start));
    console.log("随机", start, end, ret);
    return ret;
}

function randPos(pos) {
    let x = rand(pos.x - pos.x_bias, pos.x + pos.x_bias);
    let y = rand(pos.y - pos.y_bias, pos.y + pos.y_bias);
    console.log("坐标", x, y);
    return { x: x, y: y };
}

function rendorBox(top_left, bottom_right) {
    width = Math.abs((bottom_right.x + bottom_right.x_bias) - (top_left.x - top_left.x_bias));
    height = Math.abs((bottom_right.y + bottom_right.y_bias) - (top_left.y - top_left.y_bias));
    pos_x = (top_left.x - top_left.x_bias);
    pos_y = (top_left.y - top_left.y_bias);
    const w = floaty.rawWindow(
        <frame bg="#ff0000" />
    );
    w.setTouchable(true);
    w.setSize(width, height);
    //header
    w.setPosition(pos_x, pos_y - 100);
    console.log(width, height, pos_x, pos_y);

    return w;
}

// var storage = storages.create("XYZZ");
// START = false;
// var winput = floaty.window(
//     <frame gravity="center" textSize="8px" top="16sp" bg="#ffffffff">
//         <vertical>
//             <text>循环：</text>
//             <input id="loop" focusable="true" inputType="number" text="99" />
//             <button id="ok">确定</button>
//         </vertical>
//     </frame>
// );
// winput.loop.setText(LOOP.toString());
// winput.loop.on("touch_down", () => {
//     winput.requestFocus();
//     winput.loop.requestFocus();
// });
// winput.ok.on("click", () => {
//     START = true;
//     LOOP = parseInt(winput.loop.text());
//     storage.put("XYZZ_LOOP", LOOP);
//     toast("设置成功");
//     winput.disableFocus();
//     winput.loop.clearFocus();
// });

move_ahead_start = {
    x: 355,
    x_bias: 30,
    y: 1800,
    y_bias: 30,
}

move_ahead_end = {
    x: 355,
    x_bias: 30,
    y: 1700,
    y_bias: 30,
}

move_left_start = {
    x: 355,
    x_bias: 30,
    y: 1800,
    y_bias: 30,
}

move_left_end = {
    x: 300,
    x_bias: 30,
    y: 1800,
    y_bias: 30,
}

move_right_start = {
    x: 355,
    x_bias: 30,
    y: 1800,
    y_bias: 30,
}

move_right_end = {
    x: 405,
    x_bias: 30,
    y: 1800,
    y_bias: 30,
}

click_squre = {
    x: 680,
    x_bias: 30,
    y: 1485,
    y_bias: 30,
}

click_circle = {
    x: 945,
    x_bias: 30,
    y: 1485,
    y_bias: 30,
}

click_x = {
    x: 812,
    x_bias: 30,
    y: 1617,
    y_bias: 30,
}

click_triangle = {
    x: 812,
    x_bias: 30,
    y: 1350,
    y_bias: 30,
}

click_up = {
    x: 265,
    x_bias: 30,
    y: 1385,
    y_bias: 30,
}

click_down = {
    x: 265,
    x_bias: 30,
    y: 1575,
    y_bias: 30,
}

click_left = {
    x: 170,
    x_bias: 30,
    y: 1485,
    y_bias: 30,
}

click_right = {
    x: 365,
    x_bias: 30,
    y: 1485,
    y_bias: 30,
}

// rendorBox(move_ahead_end, move_ahead_start);
// rendorBox(move_left_end, move_left_start);

// rendorBox(click_triangle, click_triangle);
// rendorBox(click_x, click_x);
// rendorBox(click_squre, click_squre);
// rendorBox(click_circle, click_circle);

// rendorBox(click_up, click_up);
// rendorBox(click_down, click_down);
// rendorBox(click_left, click_left);
// rendorBox(click_right, click_right);
// sleep(5000);

while (1) {
    for (; LOOP > 0; LOOP--)
    {
        swipe(move_ahead_start.x, move_ahead_start.y, move_ahead_end.x, move_ahead_end.y, rand(5000, 6000));
        sleep(rand(6000, 7000));

        let xy = {};

        xy = randPos(click_squre);
        click(xy.x, xy.y);
        xy = randPos(click_squre);
        click(xy.x, xy.y);
        xy = randPos(click_squre);
        click(xy.x, xy.y);
        sleep(rand(4000, 5000));

        xy = randPos(click_circle);
        click(xy.x, xy.y);
        xy = randPos(click_circle);
        click(xy.x, xy.y);
        xy = randPos(click_circle);
        click(xy.x, xy.y);
        sleep(rand(10000, 12000));
    }

    sleep(rand(10000, 12000));
    swipe(move_ahead_start.x, move_ahead_start.y, move_ahead_end.x, move_ahead_end.y, 1500);
    sleep(rand(1000, 2000));
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));
    xy = randPos(click_left);
    click(xy.x, xy.y);
    sleep(rand(1000, 2000));
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(30000, 35000));

    //走到商店
    swipe(move_left_start.x, move_left_start.y, move_left_end.x, move_left_end.y, 1000);
    sleep(rand(1000, 2000));
    swipe(move_left_start.x, move_left_start.y, move_left_end.x, move_left_end.y, 1000);
    sleep(rand(1000, 2000));
    swipe(move_ahead_start.x, move_ahead_start.y, move_ahead_end.x, move_ahead_end.y, 5200);
    sleep(rand(2000, 3000));
    let xy = {};

    //打开购买
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));

    //移动到目标
    xy = randPos(click_up);
    click(xy.x, xy.y);
    sleep(rand(1000, 2000));
    xy = randPos(click_up);
    click(xy.x, xy.y);
    sleep(rand(1000, 2000));
    xy = randPos(click_up);
    click(xy.x, xy.y);
    sleep(rand(1000, 2000));

    //点击购买
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));

    //选择最大数量
    xy = randPos(click_down);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));

    //购买
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));

    //退出
    xy = randPos(click_x);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));
    xy = randPos(click_x);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));
    xy = randPos(click_x);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));

    //回到原点
    xy = randPos(click_squre);
    click(xy.x, xy.y);
    xy = randPos(click_squre);
    click(xy.x, xy.y);
    xy = randPos(click_squre);
    click(xy.x, xy.y);
    sleep(rand(1000, 2000));

    xy = randPos(click_circle);
    click(xy.x, xy.y);
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(12000, 15000));

    //走到目标
    swipe(move_left_start.x, move_left_start.y, move_left_end.x, move_left_end.y, 10000);
    sleep(rand(1000, 2000));
    swipe(move_right_start.x, move_right_start.y, move_right_end.x, move_right_end.y, 6000);
    sleep(rand(1000, 2000));
    swipe(move_ahead_start.x, move_ahead_start.y, move_ahead_end.x, move_ahead_end.y, 2000);
    sleep(rand(1000, 2000));

    //进迷宫
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(2000, 3000));
    xy = randPos(click_circle);
    click(xy.x, xy.y);
    sleep(rand(30000, 35000));

    LOOP = LOOPMAX;
}